using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using Helpers.FastToString;
using Helpers;



namespace HtmlParsing.Tegs
{
    public class TegForm : TegBase
    {
        private const string ATTR_NAME_NAME = "name";
        private const string ATTR_NAME_ID = "id";
        private const string ATTR_NAME_ACTION = "action";
        private const string ATTR_NAME_METHOD = "method";

        public TegForm(string tegString)
            : base(tegString)
        {
        }

        public override string TegName
        {
            get { return "form"; }
        }

        protected override void Parse()
        {
            Name = GetAttributeValue(ATTR_NAME_NAME, TegString);
            Action = GetAttributeValue(ATTR_NAME_ACTION, TegString);
            Id = GetAttributeValue(ATTR_NAME_ID, TegString);

            string methodName = GetAttributeValue(ATTR_NAME_METHOD, TegString);
            //Method = (methodName != null && methodName.ToUpper() == HttpMethod.POST.ToString().ToUpper()) ? HttpMethod.POST : HttpMethod.GET;
            Method = methodName;
        }

        public string CreateActionUrl(string baseUrl)
        {
            return CreateActionUrl(baseUrl, "http://");
        }

        public string CreateActionUrl(string baseUrl, string protocolName)
        {
            string rootUrl = "/";

            if (Action.Substring(0, protocolName.Length) == protocolName)
                return Action;

            if (baseUrl.Substring(0, protocolName.Length) != protocolName)
                baseUrl = protocolName + baseUrl;

            string res = null;
            if (Action.Substring(0, rootUrl.Length) != rootUrl)
            {
                res = protocolName + @"//" + baseUrl + @"/" + Action;
                res = UrlHelper.ClearUrl(res);
            }
            else
            {
                string domainName = UrlHelper.GetDomainName(baseUrl);
                if (domainName == null)
                    return null;
                res = protocolName + @"//" + domainName + "//" + Action;
                res = UrlHelper.ClearUrl(res);
            }
            return res;
        }

        private string _method = "GET";
        [PrintableAttr(PublicName = "Method")]
        public string Method
        {
            get { return _method; }
            set { _method = value; }
        }

        private string _action = "#";
        [PrintableAttr(PublicName = "Action")]
        public string Action
        {
            get { return _action; }
            set { _action = value; }
        }

        private string _name = null;
        [PrintableAttr(PublicName = "Name")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _id = null;
        [PrintableAttr(PublicName = "Id")]
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
    }
}
